/**************************************************************************//**
 * @file    FileHelpers.c
 * @author  İsmail SEZEN (email: sezenismail@gmail.com)
 * @date    02-08-2010 [04:20]
 *
 * <b>Description</b>
 *
 * This is File Helpers Library. Contains functions about file process.\n
 *
 * Copyright (C) 2009 İsmail SEZEN
 *
 * <b>License</b>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

/* Include Drectives *///{
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#include <array_helper.h>
#include <logger.h>
//}

/* Private Routines ---------------------------------------------------------------------------------*///{

/*! @brief Returns a next pointer last occurence of delimiter
 *  @param[in] str string text to be processed
 *  @param[in] delimiter This a char to search in string
 *  @return Returns a next pointer last occurence of delimiter */
static char* get_last(const char* str, char delimiter) {
	char* peek = (char*)(str + strlen(str) - 1);
	while (peek >= str) {
		if (*peek == delimiter) {
			peek++;break;
		}
		peek--;
	}
	return peek;
}

/*! @brief Redirects the stream to File
  * @param[in] fileName Filename to redirect the stream
  * @param[in] std stream to be redirected
  * @param[in] err_msg Error Messssage
  * @return Returns a pointer to the FILE stream */
static FILE* redirectStreamToFile(const char* fileName, _IO_FILE* std, const char* err_msg){
	FILE* fl = NULL;
	if(fileName != NULL)
		if( !(fl = freopen(fileName,"w",std)) ) log_err("%s",err_msg);
	return fl;

}

//}

/*! @name Public
 *  These functions are defined in the header file FileHelpers.h
 */
//@{//{

/*! @brief strips the file extension from the name
  * @sa get_last
  * @param[in] fileName File name to check
  * @return extension of file name */
char* fh_getFileExtension(char* fileName) {
	return get_last(fileName, '.');
}

/*! @brief Strips the file name from a full path string and returns a pointer to base file name char array
  * @sa get_last
  * @param[in] *path Path to check with file name or not
  * @return File name stripped from the string */
char* fh_getBaseName(char* path) {
	return get_last(path, '/');
}

/*! @brief Redirects the stdout to File
  * @sa redirectStreamToFile
  * @param[in] *fileName File name that standart output stream will be redirected
  * @return File handle to the standart stream */
FILE* fh_redirectstdoutToFile(const char* fileName) {
	return redirectStreamToFile(fileName, stdout,"Error on redirecting stdout to file! Falling back to screen mode");
}

/*! @brief Redirects the stderr to File
  * @sa redirectStreamToFile
  * @param[in] *fileName File name that standart error stream will be redirected
  * @return File handle to the standart error stream */
FILE* fh_redirectstderrToFile(const char* fileName) {
	return redirectStreamToFile(fileName, stderr,"Error on redirecting stderr to file! Falling back to screen mode");
}

/*! @brief Closes the file stream
  * @param[in] *stream Stream will be closed
  * @return void */
void fh_close(FILE* stream) {
	if(stream!=NULL){
		if(fclose(stream)) log_err("file close Error!\n");
	}
}

/*! @brief Compares extension of \a fileName to \a extension
  * @sa fh_getFileExtension
  * @param[in] fileName File name to check
  * @param[in] extension File extension
  * @return Returns an integer value that represents \a extension is equal to extension of \a filename. */
int fh_checkFileExtension(char* fileName, char* extension) {
	char* ext =fh_getFileExtension(fileName);
	return !strcmp(ext ,extension);
}

/*! @brief Counts the lines in a text file
  * @sa http://stackoverflow.com/questions/1225589/most-compact-way-to-count-number-of-lines-in-a-file-in-c
  * @param[in] fileName File to be count line numbers
  * @return Returns line count in the file */
int fh_getLineCount(char* fileName) {
	FILE *fl=fopen(fileName,"rb");
	int c=0;while(!fscanf(fl,"%*[^\n]%*c"))c++;fseek(fl,0,SEEK_SET);
	c++;
	fclose(fl);
	return c;
}

//}//@}
